package com.tarena.lbs.article.web.service;

import com.tarena.lbs.article.web.repository.ActionESRespository;
import com.tarena.lbs.article.web.repository.ArticleESRepository;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.pojo.content.entity.ActionSearchEntity;
import com.tarena.lbs.pojo.content.entity.ArticleSearchEntity;
import com.tarena.lbs.pojo.content.param.ArticleActionParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ActionService {
    @Autowired
    private ArticleESRepository articleESRepository;
    @Autowired
    private ActionESRespository actionESRepository;
    //redis的客户端 需要客户端操作hash中的数字 做自增操作 所以value序列化必须是string不能是其他
    @Autowired
    private StringRedisTemplate stringRedisTemplate;//和自动生成RedisTemplate唯一区别就是key value在java中序列化都是String
    public void actionSave(ArticleActionParam param) throws BusinessException {
        //1.封装actionSearchEntity
        ActionSearchEntity actionEntity=new ActionSearchEntity();
        //1.1 解析认证 发起行为的用户
        UserPrinciple userPrinciple= LbsSecurityContenxt.getLoginToken();
        Asserts.isTrue(userPrinciple==null,new BusinessException("-2","用户认证解析失败"));
        //1.2 拿到文档的id 查询文档 详情
        Integer id = param.getId();
        ArticleSearchEntity articleEntity = articleESRepository.findById(id).get();
        //1.3 封装具体属性
        BeanUtils.copyProperties(param,actionEntity);//action behavior comment
        actionEntity.setArticleId(id);
        actionEntity.setActionUserId(userPrinciple.getId());
        actionEntity.setArticleUserId(articleEntity.getUserId());
        actionEntity.setArticleTitle(articleEntity.getArticleTitle());
        actionEntity.setArticleLabel(articleEntity.getArticleLabel());
        actionEntity.setArticleType(articleEntity.getArticleCategoryId()+"");
        actionEntity.setCreateTime(new Date());
        log.info("用户行为数据封装:{}",actionEntity);
        //2.写入到lbs_action
        actionESRepository.save(actionEntity);

        //假设每一个 文章 count初始化都是0
        //在redis中 使用hash结构 记录某一个文章的 点赞1 收藏2 评论次数3
        String articleKey="articke:action:key:"+id;
        //拿到redis中操作hash的对象
        HashOperations opsForHash = stringRedisTemplate.opsForHash();
        //hincrby aritcleKey 1|2|3 1
        Long increment = opsForHash.increment(articleKey, param.getAction() + "", 1);
        log.info("当前文章:{},行为类型:{},自增结果:{}",id,param.getAction(),increment);


        /*//3.修改一下文章的数据 点赞 收藏 评论 次数 对原文章 likeCount accessCount favoriteCount做覆盖
        if (param.getAction().equals(1)){
            //点赞+1
            articleEntity.setLikeCount(articleEntity.getLikeCount()+1);
        }else if (param.getAction().equals(2)){
            //收藏+1
            articleEntity.setFavoriteCount(articleEntity.getFavoriteCount()+1);
        }else{
            //评论+1
            articleEntity.setAccessCount(articleEntity.getAccessCount()+1);
        }
        //更新
        articleESRepository.save(articleEntity);//不是完美的更新 实际上是 覆盖*/
    }
}

